<template>
  <div>
    <!-- 头部区域 -->
    <div class="page-header">
      <div class="title-section">
        <h1 class="page-title">订单管理</h1>
      </div>
    </div>

    <!-- 搜索区域 -->
    <div class="search_box">
      <el-form ref="searchFormRef" :model="queryInfo" label-width="120px" class="search-box">
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item prop="phone" label="客户搜索:">
              <el-input size="large" v-model.trim="queryInfo.phone" placeholder="搜索客户账号" :prefix-icon="Search"
                @change="searchTable"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item prop="status" label="订单状态:">
              <Select width="240" placeholder="请选择订单状态" v-model="queryInfo.status" :data="payStatusData"
                @change="searchTable" />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <div class="flex justify-end align-center">
              <!-- <el-button type="primary" @click="searchClick" size="large">查询</el-button> -->
              <el-button type="info" :icon="RefreshRight" size="large" @click="resetTable">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="margin-top">
      <el-table ref="multipleTableRef" :data="listData" style="width: 100%">
        <el-table-column type="selection" width="55" />

        <el-table-column property="order_no" label="订单编号" width="150"></el-table-column>
        <el-table-column label="用户信息" width="150">
          <template #default="scope">
            <div>ID：{{ scope.row.getUserInfoData?.id }}</div>
            <div>账号：{{ scope.row.getUserInfoData?.phone }}</div>
            <div>昵称：{{ scope.row.getUserInfoData?.user_name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="getOrderDataList" label="商品信息" width="300">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.getOrderDataList" :key="index" class="goods-item">
              <div><span class="label">商品名称：</span>{{ item.goods_name }}</div>
              <div><span class="label">商品类别：</span>{{ item.goods_type_name }}</div>
              <div><span class="label">商品价格：</span>${{ item.order_money }}</div>
              <div><span class="label">商品数量：</span>{{ item.order_number }}</div>
              <div v-if="item.shipping_fee > 0"><span class="label">运费：</span>{{ item.shipping_fee }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="实际付款情况" width="150">
          <template #default="scope">
            <div>订单价：{{ scope.row.OrderPayOne?.pay_money }}</div>
            <div>手续费：{{ scope.row.OrderPayOne?.fee_money }}</div>
            <div>到账：{{ scope.row.OrderPayOne?.net_money }}</div>
          </template>
        </el-table-column>

        <el-table-column property="create_time" label="下单时间"></el-table-column>

        <el-table-column property="commission" label="业务员佣金">
          <template #default="scope">
            <div>$ {{ scope.row.commission }}</div>
            <div>比例： {{ (scope.row.commission_rate *100 ).toFixed(0)}} %</div>
          </template>
        </el-table-column>
        <el-table-column property="agent_commission" label="代理佣金">
          <template #default="scope">
            <div>$ {{ scope.row.agent_commission }}</div>
            <div>比例： {{( scope.row.agent_commission_rate *100 ).toFixed(0)}} %</div>
          </template>
        </el-table-column>
        <el-table-column property="status" label="状态">
          <template #default="scope">
            <template v-for="item in payStatusData">
              <el-tag class="ml-2" :key="item.id" :color="'#FFF'" v-if="scope.row.pay_status === item.value">
                {{ item.label }}
              </el-tag>
            </template>
          </template>
        </el-table-column>
<!--        <el-table-column property="status" label="订单状态">-->
<!--          <template #default="scope">-->
<!--            <template v-for="item in statusData">-->
<!--              <el-tag class="ml-2" :key="item.id" v-if="scope.row.status === item.value">-->
<!--                {{ item.label }}-->
<!--              </el-tag>-->
<!--            </template>-->
<!--          </template>-->
<!--        </el-table-column>-->

        <!-- <el-table-column label="操作" width="300">
        <template #default="scope">
          <div class="flex align-center">
            <el-button
              class="margin-left-xs"
              size="small"
              type="danger"
              @click="viewOrderDetail(order)"
              >查看详情</el-button
            >
          </div>
        </template>
      </el-table-column> -->
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="margin-top">
      <Pagination :total="total" @change="changePage" />
    </div>

    <!-- 产品表单模态框 -->
    <ProductFormModal v-model:visible="productModalVisible" :productData="currentProduct" :isEdit="isEditMode"
      @submit="handleProductSubmit" />

    <!-- 添加价格弹窗 -->
    <ProductPriceFormModal ref="productPriceFormModalRef" @reload="searchTable" />
  </div>
</template>

<script lang="ts" setup>
import dayjs from 'dayjs'
import { ref, reactive, computed, toRef, onMounted, watch } from 'vue'
import Select from '@/components/admin/Select.vue'
import { useRoute } from 'vue-router'
import useTableHook from '@/hooks/useTableHook'
import { GetRegionOrderDoneDataListApi } from '@/api/index'
import { RefreshRight } from '@element-plus/icons-vue'
import ProductFormModal from '@/components/admin/merchants/ProductFormModal.vue'
import ProductPriceFormModal from '@/components/admin/merchants/ProductPriceFormModal.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Pagination from '@/components/admin/Pagination.vue'

const statusData = [
  {
    label: '全部状态',
    value: ''
  },
  {
    label: '待发货',
    value: 0,
    color: '#f4f4f5'
  },
  {
    label: '发货中',
    value: 1,
    color: '#409eff'
  },
  {
    label: '代签收',
    value: 2,
    color: '#e6a23c'
  },
  {
    label: '已签收',
    value: 3,
    color: '#66c03a'
  },
  {
    label: '结束订单',
    value: 4,
    color: '#909399'
  },
  {
    label: '取消订单',
    value: 5,
    color: '#f56c6c'
  }
]
const payStatusData = [
  {
    label: '待付款',
    value: 0,
    color: ''
  },
  {
    label: '付款成功',
    value: 2,
    color: '#409eff'
  },
  {
    label: '取消付款',
    value: 3,
    color: '#e6a23c'
  },
]
const queryInfo = ref({
  order_no: '',
  dates: [],
  start_time: '',
  end_time: '',
  status: '',
  page: 1,
  limit: 10,
})

const searchFormRef = ref<FormInstance>()

// 重置表格
const resetTable = () => {
  searchFormRef.value.resetFields()

  queryInfo.value.start_time = ''

  queryInfo.value.end_time = ''

  searchTable()
}

const changeDate = (record) => {
  queryInfo.value.start_time = queryInfo.value.dates[0]
  queryInfo.value.end_time = queryInfo.value.dates[1]
  searchTable()
}

onMounted(() => {
  initTable()
})

const {
  total,
  listData,
  searchTable,
  initTable,
  changePage,
} = useTableHook(queryInfo.value, GetRegionOrderDoneDataListApi, { resType: 3 })
</script>

<style lang="scss" scoped>
/* 头部区域样式 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.title-section {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  color: #2c3e50;
}

.merchant-name {
  color: #e74c3c;
}

.back-link {
  display: inline-flex;
  align-items: center;
  font-size: 14px;
  color: #7f8c8d;
  text-decoration: none;
  transition: color 0.3s;
  margin-top: 5px;
}

.back-link:hover {
  color: #e74c3c;
}

.back-link i {
  margin-right: 5px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.btn-primary {
  background-color: #e74c3c;
  color: white;
  border: none;
  padding: 10px 15px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  font-weight: 600;
  transition: background-color 0.3s;
}

.btn-primary:hover {
  background-color: #c0392b;
}

.btn-primary i {
  margin-right: 5px;
}
</style>
